package com.gameluncher;

import com.dao.RSMySQLData;
import com.gamejframe.StoneJFrame;
import com.reader.IniParser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class GameLuncherJFrame extends JFrame {
    private static boolean flag = false;
    public GameLuncherJFrame() {
        this.setTitle("MyGame");
        this.setSize(700, 500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);   //窗口居中显示
        this.setLayout(null); // 设置布局为绝对布局
        this.setVisible(true); // 显示窗体
        initLoginJFrame();
    }

    public void initLoginJFrame() {
        // 清空界面组件
        this.getContentPane().removeAll();

        // 创建账号标签
        JLabel accountLabel = new JLabel("手机号：");
        accountLabel.setBounds(175, 80, 90, 30);
        this.add(accountLabel);

        // 创建账号框
        JTextField accountField = new JTextField();
        accountField.setBounds(240, 80, 210, 30);
        this.add(accountField);

        // 创建密码标签
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setBounds(180, 130, 80, 30);
        this.add(passwordLabel);

        // 创建密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(240, 130, 210, 30);
        this.add(passwordField);

        JCheckBox rememberCheckBox = new JCheckBox("保存密码");
        rememberCheckBox.setBounds(250, 180, 100, 30);
        this.add(rememberCheckBox);

        JCheckBox autoLoginCheckBox = new JCheckBox("自动登录");
        autoLoginCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(autoLoginCheckBox.isSelected()) {
                    if(!rememberCheckBox.isSelected()) {
                        rememberCheckBox.setSelected(true);
                    }
                    rememberCheckBox.setEnabled(false);
                }else {
                    rememberCheckBox.setEnabled(true);
                }
            }
        });
        autoLoginCheckBox.setBounds(360, 180, 100, 30);
        this.add(autoLoginCheckBox);

        // 创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(240, 230, 100, 30);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取账号框内填写的值
                String accountText = accountField.getText();

                // 获取密码框内填写的值
                String passwordText = new String(passwordField.getPassword());

                // 执行登录操作
                String str = RSMySQLData.authenticateCredentials(accountText, passwordText);
                if (str.equals("登录成功")) {
                    // 如果复选框选中，则保存登录信息到配置文件a
                    if (rememberCheckBox.isSelected()) {
                        try {
                            IniParser parser = new IniParser("src/com/configurations/UserLogin.ini");
                            parser.setValue("userName", accountText);
                            parser.setValue("userPassword", passwordText);
                            if(autoLoginCheckBox.isSelected()) {
                                parser.setValue("auto", "true");
                            }else {
                                parser.setValue("auto", "false");
                            }
                            parser.save();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    flag = true;
                    UserLoginInfo.setUserPhone(accountText);
                    UserLoginInfo.setUserPassword(passwordText);
                    int id = RSMySQLData.searchID(accountText);
                    UserLoginInfo.setId(id);
                    UserLoginInfo.setUserName(RSMySQLData.searchData(id, RSMySQLData.USERNAME));
                    mainJFrame();
                } else {
                    JOptionPane.showMessageDialog(null, str);
                    System.out.println(str);
                }
            }
        });
        this.add(loginButton);

        // 创建注册按钮
        JButton gotoButton = new JButton("前往注册");
        gotoButton.setBounds(350, 230, 100, 30);
        gotoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                registerJFrame();
            }
        });
        this.add(gotoButton);

        // 将整个界面重新绘制
        this.getContentPane().repaint();

        // 获取用户登录信息
        IniParser parser = new IniParser("src/com/configurations/UserLogin.ini");
        String savedUserName = parser.getValue("userName");
        String savedUserPassword = parser.getValue("userPassword");
        String auto = parser.getValue("auto");

        // 如果用户登录信息不为空，则自动填入账号和密码，并勾选复选框
        if (savedUserName != null && savedUserPassword != null) {
            accountField.setText(savedUserName);
            passwordField.setText(savedUserPassword);
            rememberCheckBox.setSelected(true);
            // 如果已勾自动登录，则自动点击登录按钮
            if(auto.equals("true") && !flag) {
                autoLoginCheckBox.setSelected(true);
                loginButton.doClick();
            }
        }
    }


    public void registerJFrame() {
        // 清空界面组件
        this.getContentPane().removeAll();

        //创建返回按钮
        JButton backButton = new JButton("返回");
        backButton.setBounds(60, 40, 70, 20);
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initLoginJFrame();
            }
        });
        this.add(backButton);

        // 创建账号标签
        JLabel phoneLabel = new JLabel("手机号：");
        phoneLabel.setBounds(175, 60, 80, 30);
        this.add(phoneLabel);

        // 创建账号框
        JTextField phoneField = new JTextField();
        phoneField.setBounds(240, 60, 210, 30);
        this.add(phoneField);

        // 创建用户名标签
        JLabel userNameLabel = new JLabel("用户名：");
        userNameLabel.setBounds(175, 100, 80, 30);
        this.add(userNameLabel);

        // 创建用户名框
        JTextField userNameField = new JTextField();
        userNameField.setBounds(240, 100, 210, 30);
        this.add(userNameField);

        // 创建密码标签
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setBounds(180, 140, 80, 30);
        this.add(passwordLabel);

        // 创建密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(240, 140, 210, 30);
        this.add(passwordField);

        JLabel passwordLabel2 = new JLabel("再次输入密码：");
        passwordLabel2.setBounds(150, 180, 100, 30);
        this.add(passwordLabel2);

        JPasswordField passwordField2 = new JPasswordField();
        passwordField2.setBounds(240, 180, 210, 30);
        this.add(passwordField2);

        JLabel tip = new JLabel("密码不少于6位");
        tip.setBounds(240,220,200,30);
        this.add(tip);

        // 创建注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(300, 270, 100, 30);
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userPhone = phoneField.getText();
                String userName = userNameField.getText();
                String password = new String(passwordField.getPassword());
                String password2 =new String(passwordField2.getPassword());

                if(userPhone == null) {
                    JOptionPane.showMessageDialog(null, "手机号不能为空");
                    return;
                }
                if(userName == null) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空");
                    return;
                }
                if(password == null) {
                    JOptionPane.showMessageDialog(null, "密码不能为空");
                    return;
                }
                if(password.length() < 6) {
                    JOptionPane.showMessageDialog(null, "密码不能小于6位");
                    return;
                }
                if(!password.equals(password2)) {
                    JOptionPane.showMessageDialog(null, "两次密码输入不相同");
                    return;
                }
                String response = RSMySQLData.addUser(userName, password, userPhone);
                JOptionPane.showMessageDialog(null, response);

                /*if(userPhone != null) {
                    if(userName != null) {
                        if(password != null) {
                            if(password.equals(password2)) {
                                String response = RSMySQLData.addUser(userName, password, userPhone);
                                JOptionPane.showMessageDialog(null, response);
                            }else {
                                JOptionPane.showMessageDialog(null, "两次密码输入不相同");
                            }
                        }else {JOptionPane.showMessageDialog(null, "密码不能为空");}
                    }else {JOptionPane.showMessageDialog(null, "用户名不能为空");}
                }else {JOptionPane.showMessageDialog(null, "手机号不能为空");}*/

            }
        });
        this.add(registerButton);

        // 将整个界面重新绘制
        this.getContentPane().repaint();
    }

    public void mainJFrame() {
        // 清空界面组件
        this.getContentPane().removeAll();

        // 创建可点击的文本
        JLabel accountLabel = new JLabel(UserLoginInfo.getUserPhone());
        accountLabel.setBounds(10, 10, 150, 20);
        accountLabel.setForeground(Color.BLUE); // 设置文本颜色为蓝色
        accountLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 鼠标移动到文本上时显示手型光标
        accountLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                userJFrame();
            }
        });
        this.add(accountLabel);


        //创建返回按钮
        JButton backButton = new JButton("退出登录");
        backButton.setBounds(580, 10, 90, 20);
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserLoginInfo.setUserName(null);
                UserLoginInfo.setUserPhone(null);
                UserLoginInfo.setUserPassword(null);
                initLoginJFrame();
            }
        });
        this.add(backButton);

        // 创建轮播图标签
        JLabel imageLabel = new JLabel();
        imageLabel.setBounds(150, 50, 400, 225);
        imageLabel.setIcon(new ImageIcon("src/images/stone poster.png"));
        this.add(imageLabel);


        JButton StoneButton = new JButton("开始游戏");
        StoneButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StoneJFrame sj = new StoneJFrame();
                sj.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 设置新窗体关闭时的操作为释放资源
                sj.addWindowListener(new WindowAdapter() {
                    public void windowClosed(WindowEvent e) {
                        // 当新窗体关闭时执行的操作
                        GameLuncherJFrame.this.setState(JFrame.NORMAL); // 将原窗体状态设置为正常状态
                    }
                });
                sj.setVisible(true); // 显示新窗体
                GameLuncherJFrame.this.setState(JFrame.ICONIFIED); // 将原窗体状态设置为最小化
            }
        });

        StoneButton.setBounds(300, 300, 100, 30);
        this.add(StoneButton);

        // 将整个界面重新绘制
        this.getContentPane().repaint();
    }

    public void userJFrame() {
        int id = UserLoginInfo.getId();
        String userPhone = UserLoginInfo.getUserPhone();
        String userName = UserLoginInfo.getUserName();
        String userPassword = UserLoginInfo.getUserPassword();
        // 清空界面组件
        this.getContentPane().removeAll();

        JLabel txtLabel = new JLabel("用户信息");
        txtLabel.setBounds(10, 10, 150, 20);
        this.add(txtLabel);

        //创建返回按钮
        JButton backButton = new JButton("返回");
        backButton.setBounds(580, 10, 90, 20);
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mainJFrame();
            }
        });
        this.add(backButton);

        JLabel idLabel = new JLabel("ID：");
        idLabel.setBounds(175, 20, 80, 30);
        this.add(idLabel);

        JLabel idtxtLabel = new JLabel(""+id);
        idtxtLabel.setBounds(240, 20, 80, 30);
        this.add(idtxtLabel);

        // 创建账号标签
        JLabel phoneLabel = new JLabel("手机号：");
        phoneLabel.setBounds(175, 60, 80, 30);
        this.add(phoneLabel);

        // 创建账号框
        JTextField phoneField = new JTextField();
        phoneField.setText(userPhone);
        phoneField.setBounds(240, 60, 210, 30);
        this.add(phoneField);

        // 创建用户名标签
        JLabel userNameLabel = new JLabel("用户名：");
        userNameLabel.setBounds(175, 100, 80, 30);
        this.add(userNameLabel);

        // 创建用户名框
        JTextField userNameField = new JTextField();
        userNameField.setText(userName);
        userNameField.setBounds(240, 100, 210, 30);
        this.add(userNameField);

        // 创建密码标签
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setBounds(180, 140, 80, 30);
        this.add(passwordLabel);

        // 创建密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setText(userPassword);
        passwordField.setBounds(240, 140, 210, 30);
        this.add(passwordField);

        JLabel passwordLabel2 = new JLabel("再次输入密码：");
        passwordLabel2.setBounds(150, 180, 100, 30);
        this.add(passwordLabel2);

        JPasswordField passwordField2 = new JPasswordField();
        passwordField2.setBounds(240, 180, 210, 30);
        this.add(passwordField2);

        JLabel tip = new JLabel("密码不少于6位");
        tip.setBounds(240,220,200,30);
        this.add(tip);

        // 创建注册按钮
        JButton registerButton = new JButton("修改信息");
        registerButton.setBounds(300, 270, 100, 30);
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String reUserPhone = phoneField.getText();
                String reUserName = userNameField.getText();
                String rePassword = new String(passwordField.getPassword());
                String rePassword2 =new String(passwordField2.getPassword());


                if(reUserPhone.equals(userPhone)) {
                    reUserPhone = null;
                }
                if(reUserName == null) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空");
                    return;
                }
                if(reUserName.equals(userName)) {
                    reUserName = null;
                }
                if(rePassword == null) {
                    JOptionPane.showMessageDialog(null, "密码不能为空");
                    return;
                }
                if(rePassword.length() < 6) {
                    JOptionPane.showMessageDialog(null, "密码不能小于6位");
                    return;
                }
                if(rePassword.equals(userPassword)) {
                    rePassword = null;
                }
                if(!rePassword2.equals(rePassword)) {
                    JOptionPane.showMessageDialog(null, "两次密码输入不相同");
                    return;
                }
                String response = RSMySQLData.updateUser(id, reUserName, rePassword, reUserPhone);
                if (response != null) {
                    JOptionPane.showMessageDialog(null, response);
                }
            }
        });
        this.add(registerButton);





        // 将整个界面重新绘制
        this.getContentPane().repaint();
    }
}